package com.xjy.web.controller.system;

import com.xjy.common.annotation.Log;
import com.xjy.common.annotation.RepeatSubmit;
import com.xjy.common.core.controller.BaseController;
import com.xjy.common.core.domain.AjaxResult;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.enums.BusinessType;
import com.xjy.common.utils.poi.ExcelUtil;
import com.xjy.system.domain.bo.SysCycleAddBo;
import com.xjy.system.domain.bo.SysCycleEditBo;
import com.xjy.system.domain.bo.SysCycleQueryBo;
import com.xjy.system.domain.vo.SysCycleVo;
import com.xjy.system.service.ISysCycleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 收款周期Controller
 *
 * @author zc
 * @date 2022-03-08
 */
@Api(value = "收款周期控制器", tags = {"收款周期管理"})

@RestController
@RequestMapping("/system/cycle")
public class SysContractCycleController extends BaseController {
	@Autowired
	private ISysCycleService iSysCycleService;

	/**
	 * 查询收款周期列表
	 */
	@ApiOperation("查询收款周期列表")
	@PreAuthorize("@ss.hasPermi('system:cycle:list')")
	@GetMapping("/list")
	public TableDataInfo<SysCycleVo> list(@Validated SysCycleQueryBo bo) {
		return iSysCycleService.queryPageList(bo);
	}

	/**
	 * 导出收款周期列表
	 */
	@ApiOperation("导出收款周期列表")
	@PreAuthorize("@ss.hasPermi('system:cycle:export')")
	@Log(title = "收款周期", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<SysCycleVo> export(@Validated SysCycleQueryBo bo) {
		List<SysCycleVo> list = iSysCycleService.queryList(bo);
		ExcelUtil<SysCycleVo> util = new ExcelUtil<SysCycleVo>(SysCycleVo.class);
		return util.exportExcel(list, "收款周期");
	}

	/**
	 * 获取收款周期详细信息
	 */
	@ApiOperation("获取收款周期详细信息")
	@PreAuthorize("@ss.hasPermi('system:cycle:query')")
	@GetMapping("/{id}")
	public AjaxResult<SysCycleVo> getInfo(@NotNull(message = "主键不能为空")
										  @PathVariable("id") Long id) {
		return AjaxResult.success(iSysCycleService.queryById(id));
	}

	/**
	 * 新增收款周期
	 */
	@ApiOperation("新增收款周期")
	@PreAuthorize("@ss.hasPermi('system:cycle:add')")
	@Log(title = "收款周期", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody SysCycleAddBo bo) {
		return toAjax(iSysCycleService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改收款周期
	 */
	@ApiOperation("修改收款周期")
	@PreAuthorize("@ss.hasPermi('system:cycle:edit')")
	@Log(title = "收款周期", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody SysCycleEditBo bo) {
		return toAjax(iSysCycleService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除收款周期
	 */
	@ApiOperation("删除收款周期")
	@PreAuthorize("@ss.hasPermi('system:cycle:remove')")
	@Log(title = "收款周期", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iSysCycleService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}
}
